package com.jack.blog.dto;

import com.jack.blog.entity.Notice;
import com.jack.blog.utils.DateUtil;
import com.jack.blog.vo.NoticeVo;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

@Component
public class NoticeDto extends BaseDto{

    public Notice NoticeVoToNotice(int uid,NoticeVo noticeVo)throws IllegalArgumentException{
        Notice notice=new Notice();
        String content = noticeVo.getContent();
        if (content.length()>50)
            throw new IllegalArgumentException("公告内容长度超过50!");
        notice.setContent(noticeVo.getContent());
        notice.setReleaseTime(DateUtil.getTimestamp());
        notice.setUid(uid);
        return notice;
    }

    public NoticeVo NoticeToNoticeVo(Notice notice){
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setId(notice.getId());
        noticeVo.setContent(notice.getContent());
        noticeVo.setReleaseTime(DateUtil.getStringByTimestamp(notice.getReleaseTime(),"yyyy-MM-dd HH:mm"));
        noticeVo.setTop(notice.isTop());
        return noticeVo;
    }

    public List<NoticeVo> NoticeToNoticeVo(List<Notice> notices){
        List<NoticeVo> noticeVos=new LinkedList<>();
        for (Notice notice:notices)
            noticeVos.add(NoticeToNoticeVo(notice));
        return noticeVos;
    }
}
